package cyf.ParkPlace.service;

import cyf.Owner.bean.OwnerVo;
import cyf.Owner.dao.OwnerDao;
import cyf.ParkPlace.bean.Park_Owner;
import cyf.ParkPlace.dao.ParkPlaceDao;
import cyf.Tools.PAGE_MESSAGE;
import cyf.Tools.PageObject;
import cyf.Tools.ServerResponse;
import cyf.Tools.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParkPlaceService {
    @Autowired
    private ParkPlaceDao parkPlaceDao;

    private static Integer PAGE_SIZE = PAGE_MESSAGE.PAGE_SIZE;

    /**
     * 查询车位列表
     * @param param
     * @return
     */
    public Map<String,Object> getAllParkPlace(Map<String,Object> param,Integer pageCurrent) throws Exception {
        //1.验证参数的有效性
        if(pageCurrent==null||pageCurrent<1)
            pageCurrent = 1;
        //2.获取当前页数据
        //2.1 计算startIndex的值
        Integer pageSize=PAGE_SIZE;
        Integer startIndex=(pageCurrent-1)*pageSize;
        //3.获取总记录数(根据此值计算总页数)
        Integer rowCount = new Integer(0);
        if(param.get("sffp").equals(0)){
             rowCount = parkPlaceDao.getParkerNOFPCount(param);
        }else if(param.get("sffp").equals(1)){
             rowCount = parkPlaceDao.getParkerFPCount(param);
        }else if(param.get("sffp").equals(2)){
             rowCount = parkPlaceDao.getAllParkerCount(param);
        }

        PageObject pageObject=new PageObject();
        pageObject.setRowCount(rowCount);
        pageObject.setPageSize(pageSize);
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setStartIndex(startIndex);//可选
        pageObject.setPageCount(pageObject.getPageCount());
        param.put("startIndex",startIndex);
        param.put("pageSize",pageSize);
        List<Park_Owner> result = new ArrayList<>();
        //未分配
        if(param.get("sffp").equals(0)){
             result =  parkPlaceDao.getParkerNOFP(param);
             for(Park_Owner p : result){
                 p.setOwner_name("暂无");
                 p.setOwner_phone("暂无");
                 p.setSffp("未分配");
                 p.setCar_plate("暂无");
            }
        }else if(param.get("sffp").equals(1)){      //已经分配
             result =  parkPlaceDao.getParkerFP(param);
            for(Park_Owner p : result){
                p.setSffp("已分配");
            }
        }else if(param.get("sffp").equals(2)) {      //全部
             result = parkPlaceDao.getAllParker(param);
            for(Park_Owner p : result){
                p.setSffp("已分配");
                if(p.getCar_plate() == null || "".equals(p.getCar_plate())){
                    p.setCar_plate("暂无");
                    p.setSffp("未分配");
                }
                if(p.getOwner_name() == null || "".equals(p.getOwner_name())){
                    p.setOwner_name("暂无");
                }
                if(p.getOwner_phone() == null || "".equals(p.getOwner_phone())){
                    p.setOwner_phone("暂无");
                }
            }
        }
        Map<String,Object> map = new HashMap<>();
        if(result.size()>0){
            ServerResponse<List<Park_Owner>> serverResponse = ServerResponse.createServerRespsonse(1,"查询到列表",result);
            map.put("serverResponse",serverResponse);
        }else{
            ServerResponse<List<Park_Owner>> serverResponse = ServerResponse.createServerRespsonse(0,"没有数据",result);
            map.put("serverResponse",serverResponse);
        }
        map.put("pageObject",pageObject);
        map.put("pagesize",PAGE_SIZE);
        return map;
    }

    /**
     *  删除一个车位上的车辆
     */
    public Map<String,Object> deleteParkPlace(int id){

        Map<String,Object> result = new HashMap<>();
        Map<String,Object> param = new HashMap<>();
        param.put("car_parkplace_id",id);
        boolean isSucces = parkPlaceDao.deleteParkPlace(param);
        result.put("success",isSucces);

        return  result;
    }

    /**
     * 为一个车位分配汽车
     */
    public Map<String,Object> FPParkPlace(String car_plate,int car_parkplace_id){
        Map<String,Object> param = new HashMap<>();
        param.put("car_plate",car_plate);
        param.put("car_parkplace_id",car_parkplace_id);
        boolean isSuccess = parkPlaceDao.ParkPlaceFP(param);
        Map<String,Object> result = new HashMap<>();
        result.put("success",isSuccess);
        return result;
    }

    /**
     * 获取所有车位区域信息
     */
    public Map<String,Object> QueryParkArea(){
        List<Park_Owner> list = parkPlaceDao.QueryParkArea();
        Map<String,Object> result = new HashMap<>();
        boolean isSuccess = true;
        if(list.size() <= 0){
            isSuccess = false;
            result.put("success",isSuccess);
            result.put("message","没有区域信息！");
            return result;
        }
        result.put("parkareas",list);
        result.put("success",isSuccess);
        return result;
    }

    /**
     * 插入一个车位信息
     */
     public Map<String,Object> insertParkPlace(String park_area,String park_number){
         Map<String,Object> result = new HashMap<>();
         if(StringHelper.isEmpty(park_area) || StringHelper.isEmpty(park_number)){
             result.put("mes","请填写所有车位区域或号码");
             result.put("isNull",true);
             return result;
         }

         Map<String,Object> params = new HashMap<>();
         params.put("park_area",park_area);
         params.put("park_number",park_number);
         boolean repeat = this.parkPlaceDao.existPark(params);
         if(repeat){
             result.put("repeat",repeat);
             result.put("mes","该车位已经存在！");
             return result;
         }
         boolean insetrue = this.parkPlaceDao.insertParkPlace(params);
         result.put("success",insetrue);
         if(insetrue){
             result.put("mes","成功新增车位"+park_area+park_number);
         }else{
             result.put("mes","系统错误，新增车位失败");
         }
         return result;
     }
}
